﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TienTeTuong.DTO;

namespace TienTeTuong
{
    public partial class ArticleDetail : System.Web.UI.Page
    {
        public ArticleDTO article = new ArticleDTO();
        public List<ArticleDTO> newArticles = new List<ArticleDTO>();
        public List<CommentDTO> articleComments = new List<CommentDTO>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Page.Request["id"]))
            {
                Response.Redirect("Articles.aspx");
                Response.End();
            }
            else
            {
                var artId = Request["id"];
                ArticleBUS bus = new ArticleBUS();

                if (Page.Request["btn_send"] != null)
                {
                    CommentDTO cmt = new CommentDTO()
                                         {
                                             ArtID = artId,
                                             Content = Request["txt_cmt"],
                                             PEmail = Request["txt_email"],
                                             PName = Request["txt_name"]
                                         };
                    bus.InsertNewCommentByArticleID(artId, cmt);
                }
                
                article = bus.GetArticleDetailById(artId);

                newArticles = bus.GetNewArticles(3, artId);
                articleComments = bus.GetCommentByArticleID(artId, 10);

                ArrayList arrViewedArticle = new ArrayList();
                if (Session["ViewedArticle"] == null)
                {
                    bus.UpdateViewedNumberByArticleID(artId, (double)(article.Viewed + 1));
                    arrViewedArticle.Add(artId);
                    Session["ViewedArticle"] = arrViewedArticle;
                }
                else
                {
                    arrViewedArticle = (ArrayList)Session["ViewedArticle"];
                    if (!arrViewedArticle.Contains(artId))
                    {
                        bus.UpdateViewedNumberByArticleID(artId, (double)(article.Viewed + 1));
                        arrViewedArticle.Add(artId);
                        Session["ViewedArticle"] = arrViewedArticle;
                    }

                }
                
            }
        }
    }
}